var express  = require("express");
var ejs_locals = require("ejs-locals");
var flash = require("connect-flash");
var app = express();

app.set("port", process.env.PORT || 3000);


app.set("views",__dirname+"/views");
app.engine("ejs",ejs_locals);
app.set("view engine","ejs");
app.use(express.favicon());
app.use(express.logger("dev"));
app.use(express.bodyParser({uploadDir:"./public/upload"}));
app.use(express.methodOverride());
app.use(flash());
app.use(express.cookieParser());
app.use(express.session({secret:"chc project node",cookie:{secure:true},cookie:{maxAge:1800000}})); // 一天86400000
app.use(app.router);
app.use(express.static(__dirname+"/public"));

app.use(function(req, res, next){
	res.locals.user=req.session.user;
    res.locals.error=req.flash('error').length?req.flash('error'):null;
    res.locals.success=req.flash('success').length?req.flash('success'):null;
    next();
});

// 404
app.use(function(req, res, next){
	res.render("404",{
		status:404, 
		url:req.url
	});
});

app.use(function(err, req, res, next){
	res.render("500",{
		status:err.status || 500,
		error:err
	})
});

app.configure("development", function(){
	app.use(express.errorHandler());
})

//require  ctrls
var userCtrl = require("./ctrls/userCtrl");
var mailerCtrl = require("./ctrls/mailerCtrl");
var fileCtrl = require("./ctrls/fileCtrl");
// require ctrls end

app.get("/",function(req, res){
    var user = req.session.user;
    console.log("session ",user);
	res.render("index",{user:user});
});

app.post("/user/login",userCtrl.login);// 用户登录数据
app.post("/user/sign",userCtrl.sign);// 用户登录数据

app.get("/mailer/sendPage",mailerCtrl.mailerPage); // 发送邮件的页面
app.post("/mailer/send",mailerCtrl.mailerSend); // 发送邮件信息

app.get("/filePrewiew/page",fileCtrl.picPreview); // 图片预览页面
app.get("/fileUpload/page",fileCtrl.uploadPage);// 获取上传文件的页面
app.post("/file/upload",fileCtrl.upload); // 上传文件


app.listen(app.get("port"),function(){
	console.log("server listening on port "+app.get("port"));
});